import request from '@/utils/request'
import store from '../store'

/**
 *
 * 小程序授权&版本相关
 */
// 授权
export function getWechatAuthUrl(params) {
  return request({
    url: '/wx/biz/auth/pre_auth_url',
    method: 'get',
    params: params
  })
}

export function checkAuth(preAuthCode) {
  return request({
    url: '/wx/biz/auth/check_auth',
    method: 'get',
    params: { preAuthCode }
  })
}

// 拉取公众号授权信息
export function getAuthMp(params) {
  return request({
    url: '/wx/biz/authorization/mp',
    method: 'get',
    params: params
  })
}

// 拉取小程序授权信息
export function getAuthMa(params) {
  return request({
    url: '/wx/biz/authorization/ma',
    method: 'get',
    params: params
  })
}

// 拉取授权小程序详情
export function getMaDetail(params) {
  return request({
    url: '/wx/biz/wxapp/info',
    method: 'get',
    params: params
  })
}

// 发布小程序
export function updateWxappVersion(appId) {
  return request({
    url: '/wx/biz/wxapp/update_wxapp_version',
    method: 'post',
    data: { appId }
  })
}

// 刷新小程序审核状态
export function refreshWxappAuditStatus(appId) {
  return request({
    url: '/wx/biz/wxapp/refresh_audit_status',
    method: 'post',
    data: { appId }
  })
}

// 拉取授权小程序二维码
export function getMaQrcode(appId) {
  return request({
    url: '/wx/biz/wxapp/qrcode',
    method: 'get',
    params: { appId }
  })
}

// 拉取授权小程序二维码
export function getMaPreviewQrcode(appId) {
  return request({
    url: '/wx/biz/wxapp/previewQrcode',
    method: 'get',
    params: { appId }
  })
}

/**
 *
 * 小程序模板相关
 */
// 获取小程序主题模板
export function getWxappThemeCode(params) {
  return request({
    url: '/wx/biz/wxapp/theme/current',
    method: 'get',
    params: params
  })
}

// 设置小程序主题模板
export function saveWxappThemeCode(params) {
  return request({
    url: '/wx/biz/wxapp/theme/current',
    method: 'post',
    params: params
  })
}

// 获取小程序主题色
export function getWxappThemeColor(params) {
  return request({
    url: '/wx/biz/wxapp/theme/color',
    method: 'get',
    params: params
  })
}

// 设置小程序主题色
export function saveWxappThemeColor(data) {
  return request({
    url: '/wx/biz/wxapp/theme/color',
    method: 'post',
    data: data
  })
}

// 获取小程序启动页
export function getWxappSplashScreen(params) {
  return request({
    url: '/wx/biz/wxapp/theme/splash_screen',
    method: 'get',
    params: params
  })
}

// 设置小程序启动页
export function saveWxappSplashScreen(data) {
  return request({
    url: '/wx/biz/wxapp/theme/splash_screen',
    method: 'post',
    data: data
  })
}

// 获取小程序tabBarList
export function getWxappTabBarList(params) {
  return request({
    url: '/wx/biz/wxapp/theme/tab_bar',
    method: 'get',
    params: params
  })
}

// 保存小程序tabBarList
export function saveWxappTabBarList(params, data) {
  return request({
    url: '/wx/biz/wxapp/theme/tab_bar',
    method: 'post',
    params: params,
    data: data
  })
}

// 获取当前小程序页面配置(banner， 功能入口，服务直达入口等)
export function getWxappPagesContent(params) {
  return request({
    url: '/wx/biz/wxapp/theme/content_config',
    method: 'get',
    params: params
  })
}

// 获取小程序入口选项(banner， 功能入口，服务直达入口等)
export function getWxappPagesEntrances(params) {
  return request({
    url: '/wx/biz/wxapp/theme/entrances',
    method: 'get',
    params: params
  })
}

// 新增  更新 小程序功能入口 服务入口 banner
export function editWxappThemeFields({ method, data, params }) {
  return request({
    url: '/wx/biz/wxapp/theme/field',
    method: method,
    data: data,
    params: params
  })
}

// 保存卡面
export function saveWxappCardFields(data) {
  return request({
    url: '/wx/biz/wxapp/theme/card_fields',
    method: 'post',
    data: data
  })
}

// 保存海报
export function saveWxappMyPageBg(data) {
  return request({
    url: '/wx/biz/wxapp/theme/myPageBg',
    method: 'post',
    data: data
  })
}

// 保存排序
export function saveWxappFeatureRanking(data) {
  return request({
    url: '/wx/biz/wxapp/theme/batchRanking',
    method: 'post',
    data: data
  })
}

/**
 *
 * 菜单
 */
// 获取当前保存的公众号菜单
export function getPublicMenuByData() {
  const appId = store.state.wechat.mpInfo.default.appId
  return request({
    url: '/wx/biz/mp/menu/' + appId,
    method: 'get'
  })
}

// 保存发布菜单
export function savePublicMenu(buttons) {
  const appId = store.state.wechat.mpInfo.default.appId
  return request({
    url: '/wx/biz/mp/menu/' + appId,
    method: 'post',
    data: buttons
  })
}

// 同步当前实时的公众号菜单
export function getPublicMenuByWechat() {
  const appId = store.state.wechat.mpInfo.default.appId
  return request({
    url: '/wx/biz/mp/menu/current',
    method: 'get',
    params: { appId }
  })
}

// 根据key获取对应素材信息
export function getMaterialByKey(key) {
  return request({
    url: '/wx/biz/mp/message/clickContent',
    method: 'get',
    params: { key }
  })
}

// 保存菜单与对应的回复内容的映射
export function setKeyByMaterial(params) {
  params.appId = store.state.wechat.mpInfo.default.appId
  return request({
    url: '/wx/biz/mp/message/clickContent',
    method: 'post',
    data: params
  })
}

/**
 *
 * 素材
 */
// 获取图文素材列表
export function getMaterialList(params) {
  return request({
    url: '/wx/biz/mp/message/articles',
    method: 'get',
    params: params
  })
}

// 根据ID删除图文素材
export function delMaterialById(id) {
  return request({
    url: '/wx/biz/mp/message/article/' + id,
    method: 'delete'
  })
}

// 新增图文素材素材(有ID则是编辑)
export function addMaterial(data) {
  // title, picUrl, description, url
  return request({
    url: '/wx/biz/mp/message/article',
    method: 'post',
    data: data
  })
}

/**
 *
 * 文本消息
 */
// 获取公众号下配置的回复内容
export function getReplylList(page = 1, pageSize = 10) {
  const params = {}
  params.appId = store.state.wechat.mpInfo.default.appId
  params.page = page
  params.pageSize = pageSize
  return request({
    url: '/wx/biz/mp/message/replyInfoList',
    method: 'get',
    params: params
  })
}

// 增加或者修改公众号下配置的回复内容
export function addReply(form) {
  // appId, content, id, keyword,type
  form.appId = store.state.wechat.mpInfo.default.appId
  return request({
    url: '/wx/biz/mp/message/replyInfo',
    method: 'post',
    data: form
  })
}

// 根据ID删除公众号设置的文本回复
export function delReply(id) {
  const params = {}
  params.appId = store.state.wechat.mpInfo.default.appId
  params.id = id
  return request({
    url: '/wx/biz/mp/message/replyInfo',
    method: 'delete',
    params: params
  })
}

// 根据关键字查询对应的回复内容
export function searchReply(keyword) {
  const params = {}
  params.appId = store.state.wechat.mpInfo.default.appId
  params.keyword = keyword
  return request({
    url: '/wx/biz/mp/message/keyReply',
    method: 'get',
    params: params
  })
}

